package com.basic.tool.service.impl;

import com.basic.common.config.BasicConfig;
import com.basic.common.constant.Constants;
import com.basic.common.enums.BusinessStatus;
import com.basic.common.utils.SecurityUtils;
import com.basic.common.utils.StringUtils;
import com.basic.common.utils.file.FileUtils;
import com.basic.common.websocket.WebSocketUsers;
import org.springframework.stereotype.Service;
import com.basic.common.core.service.impl.BaseServiceImpl;
import com.basic.tool.domain.SysDownloadCenter;
import com.basic.tool.service.ISysDownloadCenterService;
import com.basic.tool.mapper.SysDownloadCenterMapper;

import javax.annotation.Resource;
import java.util.List;

/**
 * 下载中心Service业务层处理
 *
 * @author zhaoxy_jn@163.com
 */
@Service
public class SysDownloadCenterServiceImpl extends BaseServiceImpl<SysDownloadCenterMapper,SysDownloadCenter> implements ISysDownloadCenterService {

    @Resource
    private SysDownloadCenterMapper downloadCenterMapper;

    @Override
    public int delete(Object id) {
        SysDownloadCenter center = get(id);
        int cnt = super.delete(id);
        if(cnt > 0){
            if(StringUtils.isNotNull(center)){
                // 删除文件
                FileUtils.deleteFile(BasicConfig.getProfile() + StringUtils.substringAfter(center.getFileUrl(), Constants.RESOURCE_PREFIX));
            }
        }
        // 任务结束，发送消息
        WebSocketUsers.sendMessageToUserByText(SecurityUtils.getUserId(), "下载中心文件删除成功", BusinessStatus.SUCCESS);
        return cnt;
    }


    @Override
    public List<String> getListFor7Days() {
        return downloadCenterMapper.getListFor7Days();
    }
}
